//
//  RegisterVC.swift
//  whale
//
//  Created by topnet on 2022/3/29.
//

import UIKit

class RegisterVC: LoginBaseVC {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func actionNext() {
        super.actionNext()
        if !agreementV.btn.isSelected {
            return
        }
        LogUtil.log(["登录成功": true])
        self.navigationController?.pushViewController(MeVC(), animated: true)
    }
    
    override func initUI() {
        super.initUI()
        titleLbl.text = "注册"
        nextBtn.setTitle("注册", for: .normal)
        setupLoginText(false)
        setupAgreementText()
        contentV.da.addSubviews(sub: [accountTf,
                                      pwdTf,
                                      agreementV,
                                      nextBtn,
                                      loginLbl])
    }
    
    override func initFrame() {
        super.initFrame()
        accountTf.left_right_offsetTop(titleLbl, x: 30.adapter).height(44.adapter)
        pwdTf.left_right_height_offsetTop(accountTf, x: 20.adapter)
        agreementV.left(accountTf.frame.minX-5.adapter).top(pwdTf.frame.maxY+10.adapter).height(30.adapter).right(accountTf.frame.maxX)
        nextBtn.top(agreementV.frame.maxY + 30.adapter)
        loginLbl.left_right_offsetTop(nextBtn, x: 10.adapter).height(20.adapter)
    }
    
    private func textChange(_ str: String?) {
        if ((accountTf.text?.count ?? 0) > 0) && ((pwdTf.text?.count ?? 0) > 0) {
            nextBtn.isEnabled = true
        }else {
            nextBtn.isEnabled = false
        }
    }
    
    lazy var accountTf: LoginTextField = {
        let tf = LoginTextField(isPwd: false)
        tf.font = .font(15)
        tf.hintText = "请输入11位手机号"
        tf.rx.text.orEmpty.changed.subscribe { [weak self] str in
            self?.textChange(str)
        }.disposed(by: disposeBag)
        return tf
    }()
    
    lazy var pwdTf: LoginTextField = {
       let tf = LoginTextField(isPwd: true)
        tf.font = .font(15)
        tf.hintText = "密码"
        tf.isRightTextClosure = { [weak self] isRight, _ in
            self?.nextBtn.isEnabled = isRight
        }
        tf.rx.text.orEmpty.changed.subscribe { [weak self] str in
            self?.textChange(str)
        }.disposed(by: disposeBag)
        return tf
    }()

    
}
